<template>
    <div class="app-container">
        <div class="top">
            <div>
                <img src="../assets/images/deviceInfoTitle.png" alt="">
            </div>
            <div>备件出库</div>
        </div>
        <div class="container">
            <div class="search">
                <div class="search-buttons"> <el-button v-hasPermi="['deviceOut:deviceOut:add']" type="primary"
                        size="mini" @click="addInData">新增</el-button>
                    <el-button size="mini" @click="exportData"
                        v-hasPermi="['deviceOut:deviceOut:export']">导出</el-button>
                </div>
                <div class="search-form">
                    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                        <el-form-item label="单号" prop="orderNo">
                            <el-input v-model="queryParams.orderNo" placeholder="请输入" clearable
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                        <el-form-item label="仓库名称" prop="warehouseName">
                            <el-input v-model="queryParams.warehouseName" placeholder="请输入" clearable
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                        <el-form-item label="领用人" prop="receiverName">
                            <el-input v-model="queryParams.receiverName" placeholder="请输入" clearable
                                @keyup.enter.native="handleQuery" />
                        </el-form-item>
                        <el-form-item label="状态" prop="status">
                            <el-select v-model="queryParams.status">
                                <el-option v-for="value in typeList" :value="value.id" :key="value.id"
                                    :label="value.name"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item label="时间">
                            <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd"
                                type="daterange" range-separator="-" start-placeholder="开始日期"
                                end-placeholder="结束日期"></el-date-picker>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" size="mini" @click="handleQuery">查询</el-button>
                            <el-button size="mini" @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                </div>
            </div>
            <el-divider></el-divider>
            <div class="table-container">
                <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
                      <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="单号" align="center" prop="orderNo" min-width="170"
                        :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <!-- 触发 popover 的元素 -->
                            <el-popover placement="top" width="800" trigger="click" @show="getOrderDetail(scope.row)">
                                <!-- 自定义弹窗内容 -->
                                <div class="order-popover">
                                    <el-table stripe :data="stockInFo.stockOutDetails" style="width: 100%"
                                        ref="previewTable">
                                        <el-table-column label="序号" width="50" align="center">
                                            <template slot-scope="scope">{{ scope.$index + 1 }}</template>
                                        </el-table-column>
                                        <el-table-column label="备件名称" prop="partName" align="center" min-width="120"
                                            :show-overflow-tooltip="true"></el-table-column>
                                        <el-table-column label="编码" prop="partCode" align="center" min-width="120"
                                            :show-overflow-tooltip="true"></el-table-column>
                                        <el-table-column label="型号" prop="specModel" align="center" min-width="120"
                                            :show-overflow-tooltip="true"></el-table-column>
                                        <el-table-column label="单位" prop="unit" align="center"></el-table-column>
                                        <el-table-column label="数量" align="center" prop="quantity">
                                            <template slot-scope="scope">{{ Number(scope.row.quantity) }}</template>
                                        </el-table-column>
                                        <el-table-column label="单价/元" align="center">
                                            <template slot-scope="scope">{{ Number(scope.row.unitPrice) }}</template>
                                        </el-table-column>

                                        <el-table-column label="总价/元" align="center">
                                            <template slot-scope="scope">
                                                {{ (Number(scope.row.unitPrice || 0) * Number(scope.row.quantity || 0))
                                                }}
                                            </template>
                                        </el-table-column>
                                    </el-table>
                                </div>

                                <!-- 表格里真正显示的“单号” -->
                                <span slot="reference" class="specialTitle click-text">
                                    {{ scope.row.orderNo }}
                                </span>
                            </el-popover>
                        </template>
                    </el-table-column>

                    <el-table-column label="仓库名称" align="center" prop="warehouseName" min-width="150"
                        :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <div class="specialTitle">{{ scope.row.warehouseName }}</div>
                        </template>
                    </el-table-column>

                    <el-table-column label="提交日期" align="center" prop="submitDate" min-width="160"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="领用日期" align="center" prop="outDate" min-width="160"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="提交人" align="center" prop="submitUserName" min-width="100"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="领用人" align="center" prop="receiverName" min-width="100"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="出库总额/元" align="center" prop="totalAmount" min-width="100"
                        :show-overflow-tooltip="true" />
                    <el-table-column label="状态" align="center" prop="status" min-width="80"
                        :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <!-- <div v-if="scope.row.status == '0'" style="color: #FF851D;">待提交</div> -->
                            <div v-if="scope.row.status == '1'" style="color: green;">已提交</div>
                            <div v-if="scope.row.status == '2'" style="color: #1F90FF;">草稿</div>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width"
                        fixed="right">
                        <template slot-scope="scope">
                            <el-button size="mini" type="text" v-hasPermi="['deviceOut:deviceOut:view']"
                                @click="handleView(scope.row)">查看</el-button>
                            <el-button size="mini" type="text" v-if="scope.row.status == '2'"
                                @click="handleUpdate(scope.row)"
                                v-hasPermi="['deviceOut:deviceOut:edit']">编辑</el-button>
                            <el-button v-if="scope.row.status == '0'" size="mini" type="text"
                                @click="handleSubmit(scope.row)"
                                v-hasPermi="['deviceOut:deviceOut:submit']">提交</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </div>


        <el-dialog title="详情" :visible.sync="open" width="900px" append-to-body>
            <div style="height: 70vh;overflow-y: auto;">
                <div class="summary-card" ref="summaryCard">
                    <div class="summary-row">
                        <div class="summary-item">
                            <span class="label">单号：</span>
                            <span class="value">{{ stockInFo.orderNo }} </span>
                        </div>
                        <div class="summary-item">
                            <span class="label">仓库：</span>
                            <span class="value">{{ stockInFo.warehouseName || stockInFo.warehouseCode || '-' }}</span>
                        </div>
                        <div class="summary-item">
                            <span class="label">总金额：</span>
                            <span class="value">{{ stockInFo.totalAmount }} 元</span>
                        </div>
                    </div>
                    <div class="summary-row">
                        <div class="summary-item">
                            <span class="label">提交人：</span>
                            <span class="value">{{ stockInFo.submitUserName || '-' }}</span>
                        </div>
                        <div class="summary-item">
                            <span class="label">领用人：</span>
                            <span class="value">{{ stockInFo.receiverName || '-' }}</span>
                        </div>
                        <div class="summary-item">
                            <span class="label">提交时间：</span>
                            <span class="value">{{ stockInFo.submitDate || '-' }}</span>
                        </div>
                    </div>
                </div>
                <el-table stripe :data="stockInFo.stockOutDetails" style="width: 100%" ref="previewTable">
                    <el-table-column label="序号" width="50" align="center">
                        <template slot-scope="scope">{{ scope.$index + 1 }}</template>
                    </el-table-column>
                    <el-table-column label="备件名称" prop="partName" align="center"></el-table-column>
                    <el-table-column label="编码" prop="partCode" align="center"></el-table-column>
                    <el-table-column label="型号" prop="specModel" align="center"></el-table-column>
                    <el-table-column label="单位" prop="unit" align="center"></el-table-column>
                    <el-table-column label="数量" align="center" prop="quantity">
                        <template slot-scope="scope">{{ Number(scope.row.quantity) }}</template>
                    </el-table-column>
                    <el-table-column label="单价/元" align="center">
                        <template slot-scope="scope">{{ Number(scope.row.unitPrice) }}</template>
                    </el-table-column>

                    <el-table-column label="总价/元" align="center">
                        <template slot-scope="scope">
                            {{ (Number(scope.row.unitPrice || 0) * Number(scope.row.quantity || 0)) }}
                        </template>
                    </el-table-column>
                </el-table>
            </div>

        </el-dialog>
    </div>
</template>
<script>
import { listout, submitout, getout } from '@/api/deviceOut.js'
export default {
    name: 'deviceOut',
    data() {
        return {
            ids:[],
            // 日期范围
            dateRange: [],
            open: false,
            stockInFo: {},
            typeList: [
                // {
                //     id: 0,
                //     name: '待提交'
                // },
                {
                    id: 1,
                    name: '已提交'
                },
                {
                    id: 2,
                    name: '草稿'
                }
            ],
            userList: [],
            loading: false,
            total: 0,
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                orderNo: undefined,
                submitUserName: undefined,
                receiverName: undefined,
                warehouseName: undefined,
                status: undefined,
                params: {
                    beginTime: '',
                    endTime: ''
                }
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
           handleSelectionChange(selection) {
      this.ids = selection.map(item => item.stockOutId)
    },
        getOrderDetail(row) {
            getout(row.stockOutId).then(res => {
                this.stockInFo = res.data
            })
        },
        handleView(row) {
            getout(row.stockOutId).then(res => {
                this.stockInFo = res.data
                this.open = true
            })
        },
        handleSubmit(row) {
            row.status = 1
            this.$modal.confirm('是否确认提交此数据？').then(function () {
                return submitout(row)
            }).then(() => {
                this.getList()
                this.$modal.msgSuccess("提交成功")
            }).catch(() => { })
        },
        handleUpdate(row) {
            this.$router.push({
                path: '/addDeviceOut/index',
                query: {
                    type: 'edit',
                    stockOutId: row.stockOutId
                }
            })
        },
        exportData() {
            this.queryParams.params.beginTime = this.dateRange[0]
            this.queryParams.params.endTime = this.dateRange[1]
              this.queryParams.ids= this.ids
            this.download('outPut/out/export', {
                ...this.queryParams
            }, `出库明细_${new Date().getTime()}.xlsx`)
        },
        addInData() {
            this.$router.push({
                path: '/addDeviceOut/index',
                query: {
                    type: 'add'
                }
            })
        },
        /** 查询参数列表 */
        getList() {
            this.loading = true
            listout(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
                this.userList = response.rows
                this.total = response.total
                this.loading = false
            }
            )
        },
        // 表单重置
        reset() {
            this.form = {
                orderNo: undefined,
                submitUserName: undefined,
                warehouseName: undefined,
                status: undefined
            }
            this.resetForm("form")
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = []
            this.resetForm("queryForm")
            this.handleQuery()
        },
    }

}
</script>
<style scoped lang="scss">
.app-container {
    width: 100%;
    height: calc(100vh - 60px);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding-bottom: 10px;
}

.container {
    width: 100%;
    margin-top: 16px;
    background: #fff;
    padding: 24px 24px 0px 24px;
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .el-divider {
        margin: 20px 0 !important;
    }

    .search {
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 16px;
        justify-content: space-between;

        .search-buttons {
            flex-shrink: 0;
        }

        .search-form {
            flex: 1;
            min-width: 0;
            display: flex;
            justify-content: flex-end;

            /* 让表单内容靠右 */
            ::v-deep .el-form {
                display: flex;
                flex-wrap: wrap;
                align-items: center;
                gap: 16px;
                margin: 0;
                padding: 0;
                // justify-content: flex-end;
            }

            ::v-deep .el-form-item {
                margin: 0;
                flex-shrink: 0;
            }

            ::v-deep .el-input {
                width: 160px;
                height: 32px;
                line-height: 32px;
            }

            ::v-deep .el-form-item__label {
                font-family: PingFang SC;
                font-weight: 400;
                font-size: 14px;
                height: 32px;
                line-height: 32px;
                letter-spacing: 0%;
                vertical-align: middle;
                color: #313D4B;
            }
        }
    }

    .table-container {
        flex: 1;
        overflow: auto;
    }
}

.top {
    width: 100%;
    height: 56px;
    border-radius: 4px;
    background: rgba(255, 255, 255, 1);
    display: flex;
    align-items: center;
    padding: 0 16px;

    >div:first-child {
        width: 24px;
        height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative; // 新增

        img {
            width: 100%;
            height: 100%;
            object-fit: contain;
            position: relative;
            top: 2px; // 微调图片垂直位置
        }
    }

    >div:last-child {
        font-family: PingFang SC;
        margin-left: 8px;
        font-weight: 500;
        font-size: 18px;
        color: rgba(29, 33, 41, 1);
        line-height: 24px;
        height: 24px;
        display: flex;
        align-items: center;
    }
}

.specialTitle {
    font-family: PingFang SC;
    font-weight: 400;
    font-style: Regular;
    font-size: 14px;
    color: #41A772;
}

.summary-card {
    width: 100%;
    background: #f5f7fa;
    border-radius: 4px;
    padding: 16px;
    margin-bottom: 16px;

    .summary-row {
        display: flex;
        flex-wrap: wrap;
        gap: 20px 100px;
        margin-bottom: 20px;

        &:last-child {
            margin-bottom: 0;
        }
    }

    .summary-item {
        display: flex;
        align-items: center;
        min-width: 200px;

        .label {
            color: #4e5969;
            font-size: 14px;
            margin-right: 8px;
        }

        .value {
            color: #1d2129;
            font-weight: 500;
            font-size: 14px;
        }
    }
}
</style>
